package com.xxx.controller;

import com.xxx.controller.ex.*;
import com.xxx.controller.utils.R;
import com.xxx.pojo.User;
import com.xxx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/users")
public class UserController extends BaseController {
    @Autowired
    UserService userService;

    //注册功能
    @RequestMapping("/register")
    public R register(User user){
        System.out.println("user=======>"+user);
        userService.register(user);
        return new R(OK,"注册成功");
    }

    //登录功能
    @RequestMapping("/login")
    public R login(User user, HttpSession session){
        List<User> login = userService.login(user);
        session.setAttribute("uid",login.get(0).getUid());
        session.setAttribute("username",login.get(0).getUsername());
        return new R(OK,login.get(0));
    }
    /**
     * 修改密码功能
     */
    @RequestMapping("/updatePassword")
    public R updatePassword(String oldPassword,String newPassword,HttpSession session){
        Integer uid=getUidFromSession(session);
        String username=getUsernameFromSession(session);
        userService.updatePassword(uid,username,oldPassword,newPassword);
        return new R(OK,"修改密码成功");
    }
    /**
     * 通过uid获取用户信息功能
     */
    @RequestMapping("/getByUid")
    public R getByUid(HttpSession session){
        User user = userService.getByUid(getUidFromSession(session));
        return new R(OK,user);
    }
    @RequestMapping("/updateUserData")
    public R updateUserData(User user,HttpSession session){
        user.setUid(getUidFromSession(session));
        userService.updateUserData(user);
        return new R(OK,"修改用户信息成功");
    }
    /**
     * 上传头像功能
     */
    //设置文件上传的最大值
    public static final Integer AVATAR_MAX_SIZE=10*1024*1024;
    //限制文件上传的类型
    public static final List<String> AVATAR_TYPE=new ArrayList<>();
    static{
        AVATAR_TYPE.add("image/jpg");
        AVATAR_TYPE.add("image/png");
        AVATAR_TYPE.add("image/gif");
    }

    @Value("${avatar.savePath}")//文件保存路径
    private String savePath;
    @RequestMapping("/updateAvatar")
    public R updateAvatar(HttpSession session, MultipartFile file){
        System.out.println("===========>"+savePath);
        //判断文件是否为空
        if(file.isEmpty()){
            throw new FileEmptyException("文件为空");
        }
        //判断文件大小是否超出限制
        if(file.getSize()>AVATAR_MAX_SIZE){
            throw new FileSizeException("文件超出限制");
        }
        //判断文件类型是否正确
        if(!AVATAR_TYPE.contains(file.getContentType())){
            throw new FileTypeException("文件类型错误");
        }
        //设置 文件上传路径
        //String savePath=session.getServletContext().getRealPath("/upload");

        System.out.println("savePath======>"+savePath);
        File file1=new File(savePath);
        if(!file1.exists()){//不存在则创建
            file1.mkdir();
        }
        //使用uuid设置文件的存储名称防止重复
        String fileName=UUID.randomUUID().toString()+file.getOriginalFilename();
        //创建一个空文件
        File emptyFile=new File(savePath,fileName);
        try {
            file.transferTo(emptyFile);
        }catch (FileStateException e) {
            throw new FileStateException("文件状态异常");
        } catch (IOException e) {
            throw new FileUploadIOException("文件读写异常");
        }
        //获取uid
        Integer uid=getUidFromSession(session);
        String username=getUsernameFromSession(session);
        //返回相对路径给前端显示头像使用
        String avatarPath="/upload/"+fileName;
        userService.updateAvatar(uid,avatarPath,username);
        return new R(OK,"头像修改成功",avatarPath);
    }


}
